package com.sunny.framework.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sunny.framework.system.entity.Menu;
import com.sunny.framework.system.service.MenuService;

@Controller
@RequestMapping("home.html")
public class HomeAction {

	private Logger logger = Logger.getLogger(HomeAction.class);
	
	@Autowired
	private MenuService service;
	
	@RequestMapping(params="home")
	public String toHome(){
		logger.info("toHome");
		return "home";
	}
	
	@RequestMapping(params="menu")
	public @ResponseBody List<Menu> toJson(HttpServletRequest request){
		logger.info("menu_toJson");
		
		String parentId = request.getParameter("parentId");
		System.out.println("parentId:"+parentId);
		List<Criterion> criterions = new ArrayList<Criterion>();
		if(parentId.equals("0"))
			criterions.add(Restrictions.isNull("father"));
		else{
			criterions.add(Restrictions.eq("father.id", Long.parseLong(parentId)));
		}
		List<Menu> menus =  service.list(criterions);
		return menus;
	}
	
	@RequestMapping(params="login")
	public @ResponseBody Map<String,Object> login(HttpServletRequest request,Model model){
		Map<String,Object> map = new HashMap<String, Object>();
		String name = request.getParameter("name");
		String passwd = request.getParameter("passwd");
		System.out.println("name:"+name);
		System.out.println("passwd:"+passwd);
		map.put("success", true);
		return map;
	}
}
